package by.yakusekmaxim.bookgid.services.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import by.yakusekmaxim.bookgid.dataaccess.UserAccountDao;
import by.yakusekmaxim.bookgid.dataaccess.util.UserAccountFilter;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount_;
import by.yakusekmaxim.bookgid.services.UserAccountService;

@Service
public class UserAccountServiceImpl implements UserAccountService {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserAccountServiceImpl.class);

    @Inject
    private UserAccountDao dao;

    @Override
    public UserAccount get(Long id) {
    	UserAccount entity = dao.getById(id);
        return entity;
    }

    @Override
    public void saveOrUpdate(UserAccount userAccount) {
        if (userAccount.getId() == null) {
            LOGGER.debug("Save new: {}", userAccount);
            dao.insert(userAccount);
        } else {
            LOGGER.debug("Update: {}", userAccount);
            dao.update(userAccount);
        }
    }

    @Override
    public void delete(UserAccount userAccount) {
        LOGGER.debug("Remove: {}", userAccount);
        dao.delete(userAccount.getId());

    }

    @Override
    public void deleteAll() {
        LOGGER.debug("Remove all UserAccount");
        dao.deleteAll();
    }

    @Override
	public Long getCount() {
		return dao.getCount();
	}
    
	@Override
	public Long getCount(SingularAttribute<UserAccount, ?> attribute,Object value) {
		return dao.getCount(attribute, value);
	}

	@Override
	public List<UserAccount> getAllUserAccount(
			SingularAttribute<UserAccount, ?> attr, boolean ascending,
			int startRecord, int pageSize) {
		return dao.getAllUserAccount(attr, ascending, startRecord, pageSize);
	}

	@Override
	public List<UserAccount> getAllUserAccount(
			SingularAttribute<UserAccount, ?> attr, boolean ascending,
			int startRecord, int pageSize, UserAccountFilter filter) {
		return dao.getAllUserAccount(attr, ascending, startRecord, pageSize,filter);
	}

	@Override
	public UserAccount getAccountByEmail(String email) {
		final List<UserAccount> allByFieldRestriction = dao.getAllByFieldRestriction(UserAccount_.email, email);
		return !allByFieldRestriction.isEmpty() ? allByFieldRestriction.get(0) : null;
	}

}
